import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../../spaces/filespace/fileSpace.dart';
import '../../helper/channelHelper.dart';
import '../../helper/downloadHelper.dart';
import '../../model/ftpfile.dart';
import '../../model/jxgzp.dart';

import 'package:shared_preferences/shared_preferences.dart';
import '../../helper/routeHelper.dart';
import '../../helper/uidata.dart';

import '../../widgets/dialogWidget.dart';

import 'jxgzp_list.dart';

class JXGZPPage extends StatefulWidget {
  @override
  createState() => new JXGZPPageState();
}

class JXGZPPageState extends State<JXGZPPage> {
//  定义底部导航列表
  final List<BottomNavigationBarItem> bottomTabs = [
    // new BottomNavigationBarItem(
    // icon: new Icon(Icons.trip_origin), label:('泄洪设施')),
    new BottomNavigationBarItem(
        icon: new Icon(Icons.featured_play_list), label: ('检修工作票')),
    //  new BottomNavigationBarItem(
    // icon: new Icon(Icons.device_hub), label:('水库调度令')),
    new BottomNavigationBarItem(
        icon: new Icon(FontAwesomeIcons.file), label: ('共享文件')),
  ];

  Widget getBody(int i) {
    if (i == 0) return JxgzpListPage(false);
    //if (i == 2) return SkddlListPage();
    if (i == 1) return FileSpacePage(UIData.StationFileSpaceId, "共享文件", true);
    //if(i==1)
    //return GridTileDemo();

    // if (i == 0)
    //  return XHSSInfoPage();

    return new Container();
  }

  getNewAPP(BuildContext context) async {
    var st = await JXGZP.FindNewAPP();
    if (st!.length > 0) {
      print(st);
      var ftp1 = await FtpFile.Get(st);
      await DownloadHelper.DownLoad(context, ftp1!);
      await ChannelHelper.showToast("已下载，请到下载列表里查看");
    }
  }

  final titleList = ["检修工作票", "共享文件"];
  int currentIndex = 0; //当前索引
  late Widget currentPage; //当前页面

  @override
  void initState() {
    super.initState();
    currentPage = getBody(0);
  }

  exitLogin(BuildContext context) async {
    UIData.CurrentUser = null;
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.remove("username");
    prefs.remove("password");
    Navigator.of(context)
        .pushNamedAndRemoveUntil(UIData.loginRoute, ModalRoute.withName('/'));
  }

  @override
  Widget build(BuildContext context) {
    getNewAPP(context);

    // TODO: implement build
    return new Scaffold(
      appBar: new AppBar(
        // title: bottomTabs[currentIndex].title,
        title: Text(titleList[currentIndex]),
        actions: <Widget>[
          ElevatedButton(
            child: Icon(
              Icons.file_download,
              color: Colors.white,
            ),
            onPressed: () {
              RouteHelper.DownloadList(context);
            },
          ),
          ElevatedButton(
              child: Icon(
                Icons.exit_to_app,
                color: Colors.white,
              ),
              onPressed: () {
                showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return DialogWidget.getDialog(
                          context, "退出登录", "点击确定将退出登录", () {
                        exitLogin(context);
                      });
                    });
              })
        ],
        // leading: Icon(Icons.menu),
      ),
      bottomNavigationBar: new BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          currentIndex: currentIndex,
          items: bottomTabs,
          fixedColor: Colors.blue,
          onTap: (index) {
            setState(() {
              currentIndex = index;
              currentPage = getBody(currentIndex);
            });
          }),
      body: currentPage,
    );
  }
}
